package com.shouer.doc.request;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.shouer.doc.BuildConfig;
import com.shouer.doc.common.ShouerConst;
import com.shouer.doc.common.util.DateFormatUtil;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created by apple on 15/7/17.
 */
public abstract class BaseRequest<T> extends Request<T> {

    protected String methodName = null;

    private Map<String, String> postParam = null;

    protected String timeStamp;

    @Override
    public Map<String, String> getParams() throws AuthFailureError {
        return postParam;
    }

    public Map<String, String> getPostParam() {
        return postParam;
    }

    public void setPostParam(Map<String, String> postParam) {
        this.postParam = postParam;
    }

    public BaseRequest(Response.ErrorListener listener) {
        super(Method.POST, ShouerConst.APP_SERVER_https, listener);
        this.methodName = "";
        setRetryPolicy(new DefaultRetryPolicy(5 * 1000, 3, 1.0f));
    }

    public BaseRequest(String methodName, Response.ErrorListener listener) {
        super(Method.POST, ShouerConst.APP_SERVER_https, listener);
        this.methodName = methodName;
        setRetryPolicy(new DefaultRetryPolicy(5 * 1000, 3, 1.0f));
    }

    public BaseRequest(Response.ErrorListener listener, String url) {
        super(Method.POST, url, listener);
        this.methodName = "";
        setRetryPolicy(new DefaultRetryPolicy(5 * 1000, 3, 1.0f));
    }

    public BaseRequest(String url, String methodName, Response.ErrorListener listener) {
        super(Method.POST, url, listener);
        this.methodName = methodName;
        setRetryPolicy(new DefaultRetryPolicy(5 * 1000, 3, 1.0f));
    }

    public BaseRequest(int method, String url, Response.ErrorListener listener) {
        super(method, url, listener);
        this.methodName = "";
        setRetryPolicy(new DefaultRetryPolicy(5 * 1000, 3, 1.0f));
    }

    public BaseRequest(int method, String url, String methodName, Response.ErrorListener listener) {
        super(method, url, listener);
        this.methodName = methodName;
        setRetryPolicy(new DefaultRetryPolicy(5 * 1000, 3, 1.0f));
    }


    //重写getBody()方法  对要传输对数据的头部加上数据格式和长度固定的头消息
    @Override
    public byte[] getBody() throws AuthFailureError {
        Map<String, String> postParams = getPostParams();
        if (postParams != null) {
            JSONObject postParamObject = new JSONObject(postParams);
            timeStamp = DateFormatUtil.getCurrentSystemDateByBreakCharacter("");
            String header = RequestUtil.genHeader(timeStamp, methodName);
            String postString = RequestUtil.encryptMessage(timeStamp, header, postParamObject.toString());
            if (BuildConfig.LOG_DEBUG)
            {
                Log.d(BaseRequest.class.getSimpleName(), methodName + "请求数据:" + postString);
            }
            try {
                return postString.getBytes(getParamsEncoding());
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("Encoding not supported: " + getParamsEncoding(), e);
            }
        }

        return null;
    }
}
